//到货通知单
$(function(){
	//搜索
	$('.searchBar').click(function(){
		var thisform=$(this).parent('form');
		var picklist_order_no=thisform.find('input[name=picklist_order_no]').val();
		$('#productorderDatagrid').datagrid('options').pageNumber=1;//设置页码初始值为1
		$('#productorderDatagrid').datagrid({ 
			queryParams:{
				picklist_order_no:picklist_order_no
			}
		});
	})
	
	//更新行数据
	function productorderUpdateActions(value){
		$.post(productorderEditUrl,{
			id:value.id,
			purchasing_no:value.purchasing_no,
			section_no:value.section_no,
			pro_name:value.pro_name,
			pro_spec:value.pro_spec,
			source_no:value.source_no,
			pro_no:value.pro_no,
			batch_no:value.batch_no,
			people_make_no:value.people_make_no,
			people_purchasing_no:value.people_purchasing_no
			},function(result){
            if (!result.status){
                $.messager.alert('错误提示',result.message,'error');
            }
        },'json').error(function(data){
        	var info=eval('('+data.responseText+')');
			$.messager.confirm('错误提示',info.message,function(r){
				//$('#authAccessSetDialog').dialog('close');
			});
        });
	};

	var editIndex = undefined;
	function endEditing(){
		if (editIndex == undefined){return true}
		if ($('#productorderDatagrid').datagrid('validateRow', editIndex)){
			$('#productorderDatagrid').datagrid('endEdit', editIndex);
			editIndex = undefined;
			return true;
		} else {
			return false;
		}
	};
	//双击行编辑
	function onDblClickRow(index){
		if (editIndex != index){
			if (endEditing()){
				$('#productorderDatagrid').datagrid('selectRow', index);
				$('#productorderDatagrid').datagrid('endEdit', editIndex);
				$('#productorderDatagrid').datagrid('beginEdit', index);
				editIndex = index;
			} else {
				$('#productorderDatagrid').datagrid('selectRow', editIndex);
			}
		}
	};
	$('#productorderDatagrid').datagrid({
		//url:getproductorderUrl+"?randnum="+Math.floor(Math.random()*1000000),
		url:getproductorderUrl,
		title:'入库单列表',
		fitColumns:true,
		rownumbers:true,
		pageSize:25,
		pageNumber:1,
		pageList:[5,10,15,20,25,30,40,50],
		columns:[[
		{field:'id',title:'id',checkbox:true},
		{field:'purchasing_no',title:'入库单编号',sortable:true},
		{field:'section_no',title:'申请部门',sortable:true,
				formatter:function(value,row,index){
					// htmlobj=$.ajax({type:'post',url:getSectionNameUrl,data:{code:value},async:false});//ajax动态获取物流备案公司名称
					// var info=eval('('+htmlobj.responseText+')');
					// return info.name;
					var json = section_lists;  //创建一个json
					var len = json.length;
					for (var i =0;i<len;i++){
						if(value==json[i].no){
							return json[i].name;
						}
					}
				}
		},
		{field:'batch_no',title:'批次',sortable:true},
		{field:'people_make_no',title:'制单人',sortable:true,
				formatter:function(value,row,index){
					// htmlobj=$.ajax({type:'post',url:getPersonnelNameUrl,data:{code:value},async:false});//ajax动态获取物流备案公司名称
					// var info=eval('('+htmlobj.responseText+')');
					// return info.name;
					var json = personnel_lists;  //创建一个json
					var len = json.length;
					for (var i =0;i<len;i++){
						if(value==json[i].no){
							return json[i].name;
						}
					}
				}
		},
		{field:'people_purchasing_no',title:'申请人',sortable:true,
				formatter:function(value,row,index){
					// htmlobj=$.ajax({type:'post',url:getPersonnelNameUrl,data:{code:value},async:false});//ajax动态获取物流备案公司名称
					// var info=eval('('+htmlobj.responseText+')');
					// return info.name;
					var json = personnel_lists;  //创建一个json
					var len = json.length;
					for (var i =0;i<len;i++){
						if(value==json[i].no){
							return json[i].name;
						}
					}
				}
		},
		{field:'contacts_company_no',title:'往来单位',sortable:true,
				formatter:function(value,row,index){
					var json = contacts_company_lists;  //创建一个json
					var len = json.length;
					for (var i =0;i<len;i++){
						if(value==json[i].no){
							return json[i].name;
						}
					}
				}
		},
		{field:'mark',title:'备注',sortable:true},
		{field:'add_time',title:'添加时间',sortable:true},
		{field:'examine_time',title:'审核时间',sortable:true},
		{field:'status',title:'审核状态',sortable:true,
				formatter:function(value,row,index){
					if(value==1){
						return '通过';
					}
					if(value==2){
						return '未通过';
					}
					if(value==0){
						return '未审核';
					}

				},
		},
		{field:'inbound_status',title:'入库',sortable:true,
				formatter:function(value,row,index){
					if(value==0){
						return '否';
					}
					if(value==1){
						return '是';
					}

				}
		}
		]],
		// onDblClickRow: onDblClickRow,
		// onAfterEdit:function(index,row){
			// console.log(row);
					// row.editing = false;
					// productorderUpdateActions(row);
		// },
		onLoadError:function(data){
			var info=eval('('+data.responseText+')');
			$.messager.confirm('错误提示',info.message,function(r){
				var tab=$('#Tabs').tabs('getSelected');
				var index=$('#Tabs').tabs('getTabIndex',tab);
				$('#Tabs').tabs('close',index);
			});
		},
		rowStyler:function(index,row){
			if (row.color==0){
				return 'background-color:pink;color:blue;font-weight:bold;';
			}
		},
		pagination:true,
		fit:true,
	});
});
productorderObj={
	add:function(url){//新增数据
		$('#productorderAddForm').form('submit',{
			url:url,
            onSubmit: function(){
                return $(this).form('validate');
            },
            success: function(result){
            	var result=eval('('+result+')');
                if(!result.status){
                    $.messager.confirm('错误提示',result.message,function(r){
                    	$('#productorderAddDialog').dialog('close');  
                    });
                }else{
					$.messager.alert('提示信息',result.message,'info',function(r){
                    	$('#productorderAddDialog').dialog('close');       
						$('#productorderDatagrid').datagrid('reload'); 
                    });
                }
            },
		});
	},
	inboundBar:function(){
		var confirmArrivalRow=$('#productorderDatagrid').datagrid('getSelections');
		var text='确定入库吗！';
        if (confirmArrivalRow.length>0){
        	$.messager.confirm('入库提示',text,function(r){
                if (r){
                	var ids=[];
                	for(var i=0;i<confirmArrivalRow.length;i++){
                		ids.push(confirmArrivalRow[i].id);
						if(confirmArrivalRow[i].inbound_status==1){
							$.messager.alert('错误提示','单号"'+confirmArrivalRow[i].purchasing_no+'"已经入库，不能选择','error');
							return false;
						}
						if(confirmArrivalRow[i].status!=1){
							$.messager.alert('错误提示','单号"'+confirmArrivalRow[i].purchasing_no+'"未通过审核，不能入库','error');
							return false;
						}
                	}
                	var id=ids.join(',');
                    $.post(productinboundUrl,{id:id},function(result){
                        if (result.status){
							$.messager.confirm('错误提示',result.info,function(r){
								$('#productorderDatagrid').datagrid('reload');
							});
                            
                        }else{
                            $.messager.alert('错误提示',result.info,'error');
                        }
                    },'json').error(function(data){
                    	var info=eval('('+data.responseText+')');
						$.messager.confirm('错误提示',result.info,function(r){
							//$('#authAccessSetDialog').dialog('close');
						});
                    });	
                }
        	});
		}else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	save:function(url){//编辑后保存数据
		$('#productorderEditForm').form('submit',{
            url: url,
            onSubmit: function(){
                return $(this).form('validate');
            },
            success: function(result){
            	 var result=eval('('+result+')');
                if (!result.status){
                    $.messager.confirm('错误提示',result.message,function(r){
                    	$('#productorderEditDialog').dialog('close');
                    });
                }else{
                    $('#productorderEditDialog').dialog('close');      
                    $('#productorderDatagrid').datagrid('reload');    
                }
            }
        });
	},
	saveNums:function(url){//编辑后保存数据
		$('#productorderNumsEditForm').form('submit',{
            url: url,
            onSubmit: function(){
                return $(this).form('validate');
            },
            success: function(result){
            	 var result=eval('('+result+')');
                if (!result.status){
                    $.messager.confirm('错误提示',result.message,function(r){
                    	$('#productorderNumsEditDialog').dialog('close');
                    });
                }else{
                    $('#productorderNumsEditDialog').dialog('close');      
                    $('#productorderDatagrid').datagrid('reload');    
                }
            }
        });
	},
	saveBar:function(){//保存数据
		var editIndex = undefined;
		if (editIndex == undefined){endEditing = 1;}
		if ($('#productorderDatagrid').datagrid('validateRow', editIndex)){
			$('#productorderDatagrid').datagrid('endEdit', editIndex);
			editIndex = undefined;
			endEditing = 1;
		} else {
			endEditing = 0;
		}
		if(endEditing == 1){
			$('#productorderDatagrid').datagrid('acceptChanges');
		}
	},
	showAll:function(){//显示默认条件下所有数据
		$('#productorderDatagrid').datagrid({ queryParams:''});
	},
	infoBar:function(){//显示详情
		var productorderRow=$('#productorderDatagrid').datagrid('getSelections');
        if (productorderRow.length>1){
            $.messager.alert('提示','一次只能查看一条记录!','info');
        }else if(productorderRow.length==1){
			var id=productorderRow[0]['id'];
        	window.parent.showTab(productorderInfoUrl,'入库单详情',id);
        }else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	reloadBar:function(){//刷新数据
		$('#productorderDatagrid').datagrid('reload');
	},
	recycle:function(){//查看回收站数据
		$('#productorderDatagrid').datagrid({ queryParams:{status:-1}});
	},
	addBar:function(){//显示添加框
		$('#productorderAddDialog').dialog('open').dialog('setTitle','添加');
		$('#productorderAddForm').form('clear');
		$('#product_section_no').combobox('select',0);
		$('#product_people_make_no').combobox('select',0);
		$('#product_people_purchasing_no').combobox('select',0);
		$('#product_people_make_no').combobox('select',personnel_no);
		$('#product_people_purchasing_no').combobox('select',personnel_no);
		$('#product_contacts_company_no').combobox('select',0);
		
		$.ajax({
            type:"POST",
            url:product_ordernoUrl,
            success: function(result){
                $('#productorder_purchasing_no').textbox('setValue',result.order_no);
            }
        });
		$.ajax({
            type:"POST",
            url:product_batchnoUrl,
            success: function(result){
                $('#productorder_batch_no').textbox('setValue',result.batch_no);
            }
        });
		
	},
	editBar:function(){//显示编辑框
		var productorderRow=$('#productorderDatagrid').datagrid('getSelections');
        if (productorderRow.length>1){		            	
            $.messager.alert('提示','一次只能修改一条记录!','info');
        }else if(productorderRow.length==1){            	
        	$('#productorderEditDialog').dialog('open').dialog('setTitle','编辑');
            $('#productorderEditForm').form('load',productorderRow[0]);
        }else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	editNumsBar:function(){//显示完成数量编辑框
		var productorderRow=$('#productorderDatagrid').datagrid('getSelections');
        if (productorderRow.length>1){		            	
            $.messager.alert('提示','一次只能修改一条记录!','info');
        }else if(productorderRow.length==1){            	
        	$('#productorderNumsEditDialog').dialog('open').dialog('setTitle','编辑');
            $('#productorderNumsEditForm').form('load',productorderRow[0]);
        }else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	removeBar:function(){//删除数据
		var productorderRow=$('#productorderDatagrid').datagrid('getSelections');
        if (productorderRow.length>0){
        	$.messager.confirm('删除提示','真的要删除这'+productorderRow.length+'个信息吗?',function(r){
                if (r){
                	var ids=[];
                	for(var i=0;i<productorderRow.length;i++){
                		ids.push(productorderRow[i].id);
                	}
                	var id=ids.join(',');
                    $.post(productorderDelUrl,{id:id},function(result){
                        if (result.status){
							$.messager.alert('提示信息',result.message,'info',function(r){
								$('#productorderAddDialog').dialog('close');       
								$('#productorderDatagrid').datagrid('reload'); 
							});
                        }else{
                            $.messager.alert('错误提示',result.message,'error');
                        }
                    },'json').error(function(data){
                    	var info=eval('('+data.responseText+')');
						$.messager.confirm('错误提示',info.message,function(r){
							//$('#authAccessSetDialog').dialog('close');
						});
                    });	
                }
        	});
		}else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	clearBar:function(){//删除/清空回收站数据
		var productorderRow=$('#productorderDatagrid').datagrid('getSelections');
        if (productorderRow.length>0){
        	$.messager.confirm('清空提示','真的要清空这'+productorderRow.length+'信息吗?',function(r){
                if (r){
                	var ids=[];
                	for(var i=0;i<productorderRow.length;i++){
                		ids.push(productorderRow[i].id);
                	}
                	var id=ids.join(',');
                    $.post(productorderClearUrl,{id:id},function(result){
                        if (result.status){
							$.messager.alert('提示信息',result.message,'info',function(r){
								$('#productorderAddDialog').dialog('close');       
								$('#productorderDatagrid').datagrid('reload'); 
							});
                        }else{
                            $.messager.alert('错误提示',result.message,'error');
                        }
                    },'json').error(function(data){
                    	var info=eval('('+data.responseText+')');
						$.messager.confirm('错误提示',info.message,function(r){
							//$('#authAccessSetDialog').dialog('close');
						});
                    });	
                }
        	});
		}else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	undoBar:function(){//恢复回收站数据
		var productorderRow=$('#productorderDatagrid').datagrid('getSelections');
        if (productorderRow.length>0){
        	$.messager.confirm('恢复提示','真的要恢复这'+productorderRow.length+'信息吗?',function(r){
                if (r){
                	var ids=[];
                	for(var i=0;i<productorderRow.length;i++){
                		ids.push(productorderRow[i].id);
                	}
                	var id=ids.join(',');
                    $.post(productorderUndoUrl,{id:id},function(result){
                        if (result.status){
							$.messager.alert('提示信息',result.message,'info',function(r){
								$('#productorderAddDialog').dialog('close');       
								$('#productorderDatagrid').datagrid('reload'); 
							});
                        }else{
                            $.messager.alert('错误提示',result.message,'error');
                        }
                    },'json').error(function(data){
                    	var info=eval('('+data.responseText+')');
						$.messager.confirm('错误提示',info.message,function(r){
							//$('#authAccessSetDialog').dialog('close');
						});
                    });	
                }
        	});
		}else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	examineBar:function(value){//审核数据
		var productorderRow=$('#productorderDatagrid').datagrid('getSelections');
		if(value==1){
			var text='确定审核通过吗！';
		}
		if(value==2){
			var text='确定审核不通过吗！';
		}
        if (productorderRow.length>0){
        	$.messager.confirm('审核提示',text,function(r){
                if (r){
                	var ids=[];
                	for(var i=0;i<productorderRow.length;i++){
                		ids.push(productorderRow[i].id);
                	}
                	var id=ids.join(',');
                    $.post(productorderExamineUrl,{id:id,value:value},function(result){
                        if (result.status){
                            $('#productorderDatagrid').datagrid('reload');
                        }else{
                            $.messager.alert('错误提示',result.message,'error');
                        }
                    },'json').error(function(data){
                    	var info=eval('('+data.responseText+')');
						$.messager.confirm('错误提示',info.message,function(r){
							//$('#authAccessSetDialog').dialog('close');
						});
                    });	
                }
        	});
		}else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	printBar:function(value){
		var Row=$('#productorderDatagrid').datagrid('getSelections');

        if (Row.length>1){
			$.messager.alert('错误提示','一次只能选择一条数据','error');
		}else if(Row.length==1){
			if(value==1){
				text='生产计划单';
			}
			if(value==2){
				text='入库单';
			}
			if(value==3){
				text='转换单';
			}
			window.parent.showTab(productprintUrl,text,Row[0].id,value);
		}else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	}
}